using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Deepcode.Flux.Core.Services.FluxClient;

namespace Deepcode.Flux.Core.UI.Controls.Special
{
	public class UpdatePanels : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer
	{
		#region Child Controls
		protected PanelBox MainPanelBox;
		protected PanelBox ErrorBox;
		protected PanelBox SuccessBox;
		protected Literal _log;
		protected Literal _rollbacklog;
		protected Label _failReason;
		protected HyperLink SuccessNav;
		#endregion
		#region Child Control Creation
		protected override void CreateChildControls()
		{
			base.CreateChildControls();
			CreateMainBox();
			CreateErrorBox();
			CreateSuccessBox();
		}
		protected virtual void CreateMainBox()
		{
			// Create main box
			MainPanelBox = new PanelBox();
			MainPanelBox.Width = new Unit("500");
			Controls.Add(MainPanelBox);
			// Create pad
			PadBox pb = new PadBox();
			MainPanelBox.Controls.Add(pb);
			// Create log table literal
			Literal tableStart = new Literal();
			tableStart.Text = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr><td width=\"100%\"><b>Action</b></td><td><b>Status</b></td></tr>";
			pb.Controls.Add(tableStart);
			// Create the log
			_log = new Literal();
			_log.ID = "_log";
			pb.Controls.Add(_log);
			// Create the end table literal
			Literal tableEnd = new Literal();
			tableEnd.Text = "</table>";
			pb.Controls.Add(tableEnd);
		}
		protected virtual void CreateErrorBox()
		{
			// Create error box
			ErrorBox = new PanelBox();
			ErrorBox.Width = new Unit("500");
			ErrorBox.Title = "Installation Error";
			ErrorBox.Visible = false;
			Controls.Add(ErrorBox);
			// Create pad
			PadBox pb = new PadBox();
			ErrorBox.Controls.Add(pb);
			// Create literals and label content
			Literal start1 = new Literal();
			start1.Text = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr><td colspan=\"2\"><font color=\"red\"><b>Installation failed</b><br/>Reason:";
			pb.Controls.Add(start1);
			// Fail reason label
			_failReason = new Label();
			_failReason.ID = "_failReason";
			pb.Controls.Add(_failReason);
			// Next literal
			Literal start2 = new Literal();
			start2.Text = "</font></td></tr><tr><td width=\"100%\"><b>Rollback</b></td><td><b>Status</b></td></tr>";
			pb.Controls.Add(start2);
			// Rollback log
			_rollbacklog = new Literal();
			_rollbacklog.ID = "_rollbacklog";
			pb.Controls.Add(_rollbacklog);
			// Close table
			Literal ender = new Literal();
			ender.Text = "</table>";
			pb.Controls.Add(ender);
		}
		protected virtual void CreateSuccessBox()
		{
			// Create box
			SuccessBox = new PanelBox();
			SuccessBox.ID = "SuccessBox";
			SuccessBox.Width = new Unit("500");
			SuccessBox.Title = "Installation Success";
			SuccessBox.Visible = false;
			Controls.Add(SuccessBox);
			// Pad
			PadBox pb = new PadBox();
			SuccessBox.Controls.Add(pb);
			// Pre link literal
			Literal l1 = new Literal();
			l1.Text = "<font color=\"green\">Installation was successful</font><p>";
			pb.Controls.Add(l1);
			// Hyperlink
			SuccessNav = new HyperLink();
			SuccessNav.ID = "SuccessNav";
			SuccessNav.Text = "Click here to continue";
			pb.Controls.Add(SuccessNav);
		}
		#endregion
		#region Current Log area tracker
		private Literal _CurrentLog = null;
		private Literal CurrentLog
		{
			get
			{
				EnsureChildControls();
				if (_CurrentLog == null) _CurrentLog = _log;
				return _CurrentLog;
			}
			set
			{
				_CurrentLog = value;
			}
		}
		#endregion
		#region Mapped propertiess
		/// <summary>
		/// Specify the main title of this update panel
		/// </summary>
		public string Title
		{
			get
			{
				EnsureChildControls();
				return MainPanelBox.Title;
			}
			set
			{
				EnsureChildControls();
				MainPanelBox.Title = value;
			}
		}
		/// <summary>
		/// Specify the url that will be available after update completes successfully
		/// </summary>
		public string FinishedUrl
		{
			get
			{
				EnsureChildControls();
				return SuccessNav.NavigateUrl;
			}
			set
			{
				EnsureChildControls();
				SuccessNav.NavigateUrl = value;
			}
		}
		#endregion

		// Determine if a log item was started
		private bool bBeganItem = false;
		/// <summary>
		/// Possible condition states
		/// </summary>
		public enum States { OK, FAIL, NOSTATE };

		public void BeginItem(string message)
		{
			EnsureChildControls();
			CurrentLog.Text += "<tr><td>" + message + "</td>";
			bBeganItem = true;
		}
		public void EndItem(UpdatePanels.States state)
		{
			EnsureChildControls();
			string icon = ClientHelper.GetFluxClientFile("UpdatePanels/greydot.gif");
			string statename = "N/A";
			if (state == States.OK)
			{
				icon = ClientHelper.GetFluxClientFile("UpdatePanels/greendot.gif");
				statename = "OK";
			}
			if (state == States.FAIL)
			{
				icon = ClientHelper.GetFluxClientFile("UpdatePanels/reddot.gif");
				statename = "FAILED";
			}

			CurrentLog.Text += "<td nowrap='yes'>";
			CurrentLog.Text += String.Format("<img src='{0}' width=7 height=7 alt='{1}' border='0'/> {1}",
				icon, statename);
			CurrentLog.Text += "</td></tr>";
			bBeganItem = false;
		}
		public void BeginEndItem(string message)
		{
			BeginEndItem(message, States.NOSTATE);
		}
		public void BeginEndItem(string message, UpdatePanels.States state)
		{
			BeginItem(message);
			EndItem(state);
		}
		public void WriteNotice(string message)
		{
			EnsureChildControls();
			CurrentLog.Text += "<tr><td colspan=2><font color=red><b>" + message + "</b></font></td></tr>";
		}

		public void Success()
		{
			EnsureChildControls();
			if (bBeganItem) EndItem(States.OK);
			// Now show success panel
			this.SuccessBox.Visible = true;
		}
		public void Error(Exception e)
		{
			if (bBeganItem) EndItem(States.FAIL);
			// Now show error panel
			this._failReason.Text = e.ToString();
			this.ErrorBox.Visible = true;
			this.CurrentLog = this._rollbacklog;
		}
	}
}
